package cn.yeziji.forum.converter;

import cn.yeziji.forum.entity.ForumQuestionEntity;
import cn.yeziji.forum.utils.StrUtils;
import cn.yeziji.forum.utils.TimeUtils;
import cn.yeziji.forum.vo.question.QuestionVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 问题转换器
 *
 * @author gzkemays
 * @since 2022/11/12 6:11 PM
 */
@Mapper
public interface QuestionConverter {
  QuestionConverter INSTANCE = Mappers.getMapper(QuestionConverter.class);

  @Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
  @Mapping(target = "updateTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
  QuestionVO entityConvertToVO(ForumQuestionEntity entity);

  List<QuestionVO> entitiesConvertToVOs(List<ForumQuestionEntity> entities);

  default QuestionVO mapConvertToVOs(Map<String, Object> map) {
    QuestionVO questionVO = new QuestionVO();
    questionVO.setId((Integer) map.get("id"));
    questionVO.setNickname((String) map.get("nickname"));
    questionVO.setTitle((String) map.get("title"));
    questionVO.setContent((String) map.get("content"));
    questionVO.setViewNumber((Long) map.get("viewNumber"));
    questionVO.setCollectionNumber((Long) map.get("collectionNumber"));
    questionVO.setAnswerNumber((Long) map.get("answerNumber"));
    questionVO.setResolve(map.get("answerId") != null);
    questionVO.setToOpera((Boolean) map.get("toOpera"));
    questionVO.setCreateTime(
        StrUtils.dateToStr(TimeUtils.COMMON_SIMPLE_PATTERN, (Date) map.get("createTime")));
    questionVO.setUpdateTime(
        StrUtils.dateToStr(TimeUtils.COMMON_SIMPLE_PATTERN, (Date) map.get("createTime")));
    return questionVO;
  }

  default List<QuestionVO> mapsConvertToVOs(List<Map<String, Object>> maps) {
    List<QuestionVO> list = new ArrayList<>();
    for (Map<String, Object> map : maps) {
      list.add(mapConvertToVOs(map));
    }
    return list;
  }
}
